<?php

/**
 * @Author: king
 * @Date:   2024-05-28 21:42:59
 * @Last Modified by:   king
 * @Last Modified time: 2024-05-28 20:46:26
 */

namespace App\Http\Controllers\Api\v3;

use App\Tools\Astrocalendar;
use Illuminate\Http\Request;
use App\Models\Api\Recode;
use AstroTool\planetName;
use AstroTool\SweTestEmpty;
use App\Models\Api\CorpusConstellation;
use App\Models\Api\CorpusUser;
use App\Http\Controllers\Api\BaseController;
use App\Http\Controllers\Api\v1\Astro\NatalController;


/**
 * 星盘
 * Class Astrolabe
 * @package use App\Tools\Astrolog\Astrolog
 */
class AstroCalendarController extends BaseController
{

    //一月内相位数据
    public function Index(Request $request)
    {


        $year = $request->input('year');
        $month = $request->input('month');
        $tz=8;
        if(!empty($request->input('tz'))){
            $tz = $request->input('tz');
        }
        if ($month > 12 or $month < 1) {
            $month = 1;
        }
        if ($year > 3999 or $year < -100) {
            $year = 1;
        }

        $data =  (new Astrocalendar())->astroCalendar($year,$month,$tz);

        return show(200, '加载成功', $data);
    }


        //太阳推运盘计算
    public function sign(Request $request)
    {
        if (empty($request->input('id'))) {
            return ['code' => 1060002, 'msg' => '档案ID不能为空'];
        }

        $map = ['id' => $request->input('id', 0), 'uid' => $request->uid];

        $RecodeModel = new Recode();

        $userinfo = $RecodeModel->getRecode($map);

        $param = $request->all();
        $param['h_sys'] = 'P';

        $natalModel = new NatalController();
        $newRequest = new Request(['id' => $request->input('id')]);
        $details = $natalModel->get_total_score($newRequest);
        $second1 = 0;
        $date1 = mktime($userinfo['hour'], $userinfo['minute'], $second1, $userinfo['month'], $userinfo['day'], $userinfo['year']);
        $birthday =  $date1;
        $time_zone = $userinfo['timezone'];

        $longitude = ($userinfo['long_deg'] + $userinfo['long_min'] / 60 ) * $userinfo['ew'];
        $latitude = ($userinfo['lat_deg'] + $userinfo['lat_min'] / 60 ) * $userinfo['ns'];
        if ($longitude > 66.56 or $latitude < -66.56) {
            $param['h_sys'] = 'D';
            //return ['code' => 1060002, 'msg' => '纬度在-66.56~66.56之间'];
        }
        $birth_points = $longitude . ',' . $latitude . ',' . $param['h_sys'];
        $is_summer = 0;

        $life_birth_time = ($birthday) - $time_zone * 3600 - $is_summer * 3600;

        $utdatenow = date('d.m.Y', $life_birth_time);

        $utnow = date('H:i:s', $life_birth_time);
        $life_arr = [
            'b' => $utdatenow,
            'p' => '01234Hm',
            'house' => $birth_points . ',P',
            'ut' => $utnow,
            'f' => 'Plsj',  //名字 经度 度数经度 速度 宫位 名字id
            'g' => ',',
            'head',
            'roundsec'
        ];

        $exSweTest = new SweTestEmpty();
        $life_data = $exSweTest->calculate($life_arr, ['0', '1', '2', '3', '4', 'H', 'm', 'virtual' => ['10']]);

        $head = array();
        $sign_cur = array();
        $house_cur = array();
        $paper = '';

        $sign_phase = $life_data['sign_phase'];
        $attribute_chinese = ['变动' => 'change', '固定' => 'fixed', '本位' => 'standard', '土相' => 'soil', '水相' => 'water', '火相' => 'fire', '风相' => 'wind'];

        $attribute_corpus['change'] = '多变，不愿意受到束缚，适应力超强';
        $attribute_corpus['fixed'] = '忠诚、有恒心、可靠。';
        $attribute_corpus['standard'] = '意志力强，不受外界影响，随时准备迎接挑战';
        $attribute_corpus['soil'] = '脚踏实地的现实主义者，可靠、勤奋和务实';
        $attribute_corpus['water'] = '多情，敏感，洞察力强，追求深刻意义';
        $attribute_corpus['fire'] = '精神高尚、热情和直接的诚实';
        $attribute_corpus['wind'] = '超然、客观公正，喜欢团队合作，终生学习者';

        $hsore['east'] = '天生领导，自我激励，冒险者，愿意迎接外界挑战';
        $hsore['west'] = '喜欢团队合作，机会主义者，能充分利用机会';
        $hsore['south'] = '雄心勃勃，事业心强，希望获得名望和认可';
        $hsore['north'] = '主观，重视个人成长，喜欢一个人搞定事情';
        $house_nameList = array("一宫", "二宫", "三宫", "四宫", "五宫", "六宫", "七宫", "八宫", "九宫", "十宫", "十一宫", "十二宫");

        //"太阳","月亮","水星","金星","火星","北交","上升","婚神"

        //$corpusConstellationListWhere['type']=['in',[1,2,3,4,5,6]];
        $corpusConstellationListWhere['chartType']=1;
        $model = new CorpusConstellation();
        $data_plant = $model->where($corpusConstellationListWhere)->whereIn('type', [1, 2, 3, 4, 5, 6])->get(['id','oneself','other','type','keywords','content']);

        $zuiliangxing='';
        foreach ($life_data['planet'] as $key => $valoe) {

            foreach ($life_data['house'] as $keyh => $valueh) {

                if ($keyh < 11) {
                    $last_house = $life_data['house'][$keyh + 1];
                } else {

                    $last_house = $life_data['house'][0];
                }

                if ($valueh['longitude'] > $last_house['longitude']) {

                    if ($valoe['longitude'] < $last_house['longitude']) {
                        $valoe['longitude'] += 360;
                    }

                    $last_house['longitude'] += 360;
                }

                if ($valoe['longitude'] >= $valueh['longitude'] and $valoe['longitude'] < $last_house['longitude']) {
                    $valoe['house_id'] = $keyh + 1;
                }
            }

            $attribute[$attribute_chinese[$sign_phase[$valoe['sign']['sign_id']][0]]][] = $valoe;
            $attribute[$attribute_chinese[$sign_phase[$valoe['sign']['sign_id']][1]]][] = $valoe;

            // $data = $this->logicCorpusConstellation->getCorpusConstellationInfo(['oneself' => $valoe['planet_chinese'], 'type' => 1, 'chartType' => 1, 'degree' => -1], 'oneself,other,keywords,content');

            //'other' => $valoe['sign']['sign_chinese'],

            if (empty($sign_cur[$valoe['code_name']])) {
                $sign_cur[$valoe['code_name']] = array();
            }
            if (empty($head[$valoe['code_name']])) {
                $head[$valoe['code_name']] = array();
            }
            if (empty($head[$valoe['code_name']])) {
                $head[$valoe['code_name']] = array();
            }
            if (empty($house_cur[$valoe['code_name']])) {
                $house_cur[$valoe['code_name']] = array();
            }
            foreach ($data_plant as $kysf => $vsd) {

                if ($vsd['oneself'] == $valoe['planet_chinese']) {
                    if (!empty($vsd['keywords']) and $vsd['type'] == 5 and $valoe["sign"]["sign_chinese"] == $vsd['other']) {
                        $keywords = explode("，", $vsd['keywords']);
                        $keywords_key = rand(0, (count($keywords) - 1));
                        $vsd['keywords'] = $keywords[$keywords_key];

                        $head[$valoe['code_name']] = ['planet_id' => $valoe['code_name'], 'planet' => $valoe['planet_chinese'], 'sign_id' => $valoe["sign"]["sign_id"], 'sign' => $vsd['other'], 'keywords' => $vsd['keywords']];
                        $sign_cur[$valoe['code_name']]['planet_id'] = $valoe['code_name'];
                        $sign_cur[$valoe['code_name']]['planet'] = $valoe['planet_chinese'];
                        $sign_cur[$valoe['code_name']]['sign_id'] = $valoe["sign"]["sign_id"];
                        $sign_cur[$valoe['code_name']]['sign'] = $vsd['other'];
                        $sign_cur[$valoe['code_name']]['content'] = $vsd['content'];
                    }

                    if ($vsd['type'] == 1) {
                        $sign_cur[$valoe['code_name']]["describe"] = html_entity_decode($vsd['content']);
                    }
                    //行星落宫位
                    if ($vsd['type'] == 4 and $vsd['other'] == $valoe["house_id"]) {

                        $house_cur[$valoe['code_name']]['planet_id'] = $valoe['code_name'];
                        $house_cur[$valoe['code_name']]['planet'] = $valoe['planet_chinese'];
                        $house_cur[$valoe['code_name']]['house_id'] = $valoe["house_id"];
                        $house_cur[$valoe['code_name']]['house'] = $vsd['other'].'宫';
                        $house_cur[$valoe['code_name']]['content'] = $vsd['content'];
                       // $house_cur[$valoe['code_name']]['describe'] = '第' . $vsd['other'].'宫';
                    }
                }

                if ($vsd['type'] == 3   and $vsd['oneself'] == $valoe["house_id"]) {

                    if(!empty($house_cur[$valoe['code_name']])){
                        $house_cur[$valoe['code_name']]["describe"] = '第' . $vsd['oneself'].'宫：'.html_entity_decode($vsd['content']);
                    }
                }
            }
            if(empty($zuiliangxing) and in_array($valoe['code_name'].$valoe['sign']['sign_chinese'],['1金牛','3金牛','2双子','1巨蟹','2处女','3天秤','4天蝎','4摩羯','3双鱼'])){

                $diji=ceil($valoe['sign']['deg']/ 6);
                if($diji==0 or $diji==6){
                    $diji=1;
                }

                $SignNamesSimple = config('astrolog')['PlanetNamesSimple'];

                $zuiliangxing=$SignNamesSimple[$valoe['code_name']].$valoe['sign']['sign_chinese'].$diji;
            }
        }

        $CorpusUser = new CorpusUser();
        $xingluogong['title']='日'.$life_data['planet'][0]['sign']['sign_chinese'].'月'.$life_data['planet'][1]['sign']['sign_chinese'];
        $xingluogong['attribution']="zongjiao";
        $xingluogong['attribution_str']="星座一句话";

        $CorpusUserInfo_yiju = $CorpusUser->where($xingluogong)->first();

        $paper = $CorpusUserInfo_yiju['content1'];

        $taiyang_sign=$life_data['planet'][0]['sign']['sign_id'];
        $taiyang_sign+=6;
        $yuelia_sign=$life_data['planet'][1]['sign']['sign_id'];
        $yuelia_sign+=6;
        if($taiyang_sign>=12){$taiyang_sign=$taiyang_sign-12;}
        if($yuelia_sign>=12){$yuelia_sign=$yuelia_sign-12;}

        if($taiyang_sign==$yuelia_sign){
            $sign_code['compatibility_sign'] = $life_data["signChinese"][$taiyang_sign].'座';
        }else{
            $sign_code['compatibility_sign'] = $life_data["signChinese"][$taiyang_sign].'座、'.$life_data["signChinese"][$yuelia_sign].'座';

        }

        $yushu=date('H', $life_birth_time)% 3;
        $lastDigit = date('d', $life_birth_time) % 10; // 获取个位数
        $cor_user_where['title']=$life_data["signChinese"][$life_data['planet'][$yushu]['sign']['sign_id']].'座'.($lastDigit+1);
        $cor_user_where['attribution']='zongjiao';
        $cor_user_where['attribution_str']='九宫格星座密码';

        $corpusUserInfo = $CorpusUser->where($cor_user_where)->first();;

        $sign_code['surface'] = $corpusUserInfo['content1'];
        $sign_code['actual'] = $corpusUserInfo['content2'];
        $sign_code['hidden_skills'] = $corpusUserInfo['content3'];

        $date_reg['paper'] = $paper;


        if(empty($zuiliangxing)){

            $diji=ceil($life_data['planet'][0]['sign']['deg']/ 6);
            if($diji==0 or $diji==6){
                $diji=1;
            }
            $zuiliangxing=$life_data['planet'][0]['sign']['sign_chinese'].$diji;
        }

        $cor_user_where['title']=$zuiliangxing;
        $cor_user_where['attribution']='zongjiao';
        $cor_user_where['attribution_str']='星座关键词';
        $corpusUserInfoziwei = $CorpusUser->where($cor_user_where)->first();;

        $date_reg['supreme_planet'] = $corpusUserInfoziwei['content1'];
        $date_reg['supreme_planet_describe'] = $corpusUserInfoziwei['content2'];

        $date_reg['love_describe'] = '越爱越舒服';
        $date_reg['career_describe'] = '得天独厚';
        $date_reg['wealth_describe'] = '努力就暴富';
        $date_reg['people_describe'] = '越爱越舒服';

        $date_reg['sign_code'] = $sign_code;


        $head = array_filter($head);
        $house_cur = array_filter($house_cur);
        $sign_cur = array_filter($sign_cur);


        $date_reg['head'] = array_values($head);
        $date_reg['sign'] = array_values($sign_cur);
        $date_reg['house'] = array_values($house_cur);
        $date_reg['sign_score'] = $details->original['data']['sign_score'];
        $date_reg['planet_power'] = $details->original['data']['planet_power'];
        $date_reg['house_power'] = $details->original['data']['house_power'];
        $date_reg['total_power'] = $details->original['data']['total_power'];

       return show(200, '加载成功', $date_reg);
    }
}
